<template>
  <div>
    <el-card class="box-card" style="margin: 10px 0px">
      <!-- 头部区域 -->
      <div slot="header" class="clearfix">
        <!-- @tab-click="handleClick" -->
        <!-- 头部左侧 -->
        <el-tabs class="tab" v-model="activeName">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visit"></el-tab-pane>
        </el-tabs>
        <!-- 头部右侧 -->
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            class="date"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </div>
      </div>
      <!-- 内容区域 -->
      <div>
        <el-row :gutter="10">
          <el-col :span="18">
            <div class="charts" ref="charts"></div>
          </el-col>
          <el-col :span="6">
            <h4>门店{{ title }}排名</h4>
            <ul>
              <li>
                <span class="lindex">1</span>
                <span>麦当劳</span>
                <span class="rvalue">76354</span>
              </li>
              <li>
                <span class="lindex">2</span>
                <span>肯德基</span>
                <span class="rvalue">76345</span>
              </li>
              <li>
                <span class="lindex">3</span>
                <span>真功夫</span>
                <span class="rvalue">54343</span>
              </li>
              <li>
                <span class="lindex">4</span>
                <span>必胜客</span>
                <span class="rvalue">52347</span>
              </li>
              <li>
                <span class="lindex1">5</span>
                <span>季季红</span>
                <span class="rvalue">43543</span>
              </li>
              <li>
                <span class="lindex1">6</span>
                <span>茶百道</span>
                <span class="rvalue">34587</span>
              </li>
              <li>
                <span class="lindex1">7</span>
                <span>Coco</span>
                <span class="rvalue">34535</span>
              </li>
            </ul>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from "echarts";
import dayjs from 'dayjs'
import {mapState} from "vuex";

export default {
  data() {
    return {
      activeName: "sale",
      myCharts: null,
      // 手机日历数据
      date: []
    };
  },
  mounted() {
    this.myCharts = echarts.init(this.$refs.charts);
    this.myCharts.setOption({
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      title: {
        text: this.title + "趋势",
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "40%",
          data: [],
        },
      ],
    });
  },
  computed: {
    // 标题切换
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量';
    },
    ...mapState({
      listState: state => state.home.list
    })
  },
  watch: {
    title() {
      // 重新修改图标的配置数据
      this.myCharts.setOption({
        title: {
          text: this.title
        },
        xAxis: {
          data: this.title == '销售额' ? this.listState.orderFullYearAxis : this.listState.userFullYearAxis
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "40%",
            data: this.title == '销售额' ? this.listState.orderFullYear : this.listState.userFullYear
          }
        ]
      })
    },
    listState() {
      this.myCharts.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        title: {
          text: this.title + "趋势",
        },
        xAxis: [
          {
            type: "category",
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "40%",
            data: this.listState.orderFullYear,
          },
        ],
      });
    }
  },
  methods: {
    setDay() {
      const day = dayjs().format('YYYY-MM-DD');
      this.date = [day, day];
    },
    setWeek() {
      const weekStart = dayjs().day(1).format('YYYY-MM-DD');
      const weekEnd = dayjs().day(7).format('YYYY-MM-DD');
      this.date = [weekStart, weekEnd];
    },
    setMonth() {
      const monthStart = dayjs().startOf('month').format('YYYY-MM-DD');
      const monthEnd = dayjs().endOf('month').format('YYYY-MM-DD');
      this.date = [monthStart, monthEnd];
    },
    setYear() {
      const yearStart = dayjs().startOf('year').format('YYYY-MM-DD');
      const yearEnd = dayjs().endOf('year').format('YYYY-MM-DD');
      this.date = [yearStart, yearEnd];
    }
  }
};
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.el-tabs__item {
  font-size: 18px;
}
.right {
  position: absolute;
  right: 0px;
}
.right span {
  font-size: 14px;
  margin: 0px 10px;
}
.date {
  width: 250px;
  margin: 0px 20px;
}
.charts {
  width: 100%;
  height: 365px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0px;
}
ul li {
  height: 8%;
  line-height: 20px;
  padding: 20px 0px;
}
span:not(.lindex) {
  font-size: 16px;
  padding: 0px 15px;
}
.lindex {
  float: left;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border-radius: 50%;
  color: white;
  background: black;
}
.lindex1 {
  float: left;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border-radius: 50%;
  color: white;
  background: lightgray;
  padding: 0px !important;
}
.rvalue {
  float: right;
}
</style>
